//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2013 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_FUNCTORS_ACTORS_UNARY_H_
#define _BUOLA_FUNCTORS_ACTORS_UNARY_H_

#include <buola/buola.h>

namespace buola { namespace fn {

///\addtogroup functors
///@{

///\name Unary actors
//@{

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// tag class for unary functions which can be used as actors
///
/// This contains several function classes which can be used where a unary actor is needed. Among others, it
/// contains functions to assign values to variables and push values into containers
///
/// By inheriting from this class, it is possible to combine actors using \b operator>>
///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct GUnaryActor {};
    
/////////////////////////////////////////////////////////////////////////////////////////////////
///
/// assign-to actor
///
/// Actor which assigns a value to some object.
///
/////////////////////////////////////////////////////////////////////////////////////////////////
template<typename tType>
class AAssignTo : public GUnaryActor
{
public:
    ///constructs the actor
    ///
    ///\param pVar object to which the value will be assigned
    explicit AAssignTo(tType &pVar)
        :   mVar(pVar)
    {}
    
    ///assigns \p pVar to \p mVar
    template<typename tOther>
    void operator()(tOther &&pVar) const
    {
        mVar=std::forward<tOther>(pVar);
    }
    
private:
    tType &mVar;
};
    
/////////////////////////////////////////////////////////////////////////////////////////////////
///
/// push-back-into actor
///
/// Actor which pushes values to the back of a container.
///
/////////////////////////////////////////////////////////////////////////////////////////////////
template<typename tCont>
class APushBackInto : public GUnaryActor
{
public:
    ///constructs the actor
    ///
    ///\param pCont container into which values will be pushed
    explicit APushBackInto(tCont &pCont)
        :   mCont(pCont)
    {}
    
    ///pushes \p pValue into the container
    template<typename tValue>
    void operator()(tValue &&pValue) const
    {
        mCont.push_back(std::forward<tValue>(pValue));
    }
    
private:
    tCont &mCont;
};
    
/////////////////////////////////////////////////////////////////////////////////////////////////
///
/// push-front-into actor
///
/// Actor which pushes values to the front of a container.
///
/////////////////////////////////////////////////////////////////////////////////////////////////
template<typename tCont>
class APushFrontInto : public GUnaryActor
{
public:
    ///constructs the actor
    ///
    ///\param pCont container into which values will be pushed
    explicit APushFrontInto(tCont &pCont)
        :   mCont(pCont)
    {}
    
    ///pushes \p pValue into the container
    template<typename tValue>
    void operator()(tValue &&pValue)
    {
        mCont.push_front(std::forward<tValue>(pValue));
    }
    
private:
    tCont &mCont;
};
    
/////////////////////////////////////////////////////////////////////////////////////////////////
///
/// unary actor combiner
///
/// Combines two unary actors into a single actor, which executes both actors
///
/////////////////////////////////////////////////////////////////////////////////////////////////
template<typename tLH,typename tRH>
struct AUnaryActorCombine : public GUnaryActor
{
    ///initializes the sub-predicates from the arguments
    template<typename tALH,typename tARH>
    AUnaryActorCombine(tALH &&pLH,tARH &&pRH)
        :   mLH(std::forward<tALH>(pLH))
        ,   mRH(std::forward<tARH>(pRH))
    {}
    
    ///calls both sub-actors
    template<typename tParam>
    void operator()(tParam &&pVal) const
    {
        mLH(std::forward<tParam>(pVal));
        mRH(std::forward<tParam>(pVal));
    }
    
private:
    tLH mLH;
    tRH mRH;
};

///returns an actor which calls both actors passed as arguments
template<typename tLH,typename tRH>
inline OEnableIfTag2<tLH,tRH,GUnaryActor,AUnaryActorCombine<ODecay<tLH>,ODecay<tRH>>>
operator>>(tLH &&pLH,tRH &&pRH)
{
    return {std::forward<tLH>(pLH),std::forward<tRH>(pRH)};
}

///returns an actor which will assign its argument to \p pVar
template<typename tType>
inline AAssignTo<tType> assign_to(tType &pVar)
{
    return AAssignTo<tType>(pVar);
}

///returns an actor which will push its argument into the back of \p pCont
template<typename tCont>
inline APushBackInto<tCont> push_back_into(tCont &pCont)
{
    return APushBackInto<tCont>(pCont);
}

///returns an actor which will push its argument into the front of \p pCont
template<typename tCont>
inline APushFrontInto<tCont> push_front_into(tCont &pCont)
{
    return APushFrontInto<tCont>(pCont);
}

//@}

///@}

/*namespace fn*/ } /*namespace buola*/ }

#endif
